package com.yeung.actions;

import com.yeung.beans.Department;
import com.yeung.beans.Employee;
import com.yeung.dao.DepartmentDao;
import com.yeung.dao.EmployeeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

@Controller
public class EmployeeHandler {
    @Autowired
    private EmployeeDao employeeDao;
    @Autowired
    private DepartmentDao departmentDao;

    @RequestMapping("/emps")
    public String list(ModelMap map) {
        map.put("employees", employeeDao.getEmployess());
        return "list";
    }

    @RequestMapping("/curd")
    public String curd() {
        return "curd";
    }

    /**
     * 解决更新操作时,诸如数据与数据库数据不一致的问题
     *
     * @param id
     * @param map
     */
    @ModelAttribute
    public void getEmployee(@RequestParam(value = "id", required = false) Integer id, ModelMap map) {
        if (id != null)
            map.put("employee", employeeDao.getEmployeeById(id));

    }

    @RequestMapping(value = "/emp", method = RequestMethod.GET)
    public String input(Map<String, Object> map) {
        map.put("departments", departmentDao.getDepartments());
        map.put("employee", new Employee());
        return "input";
    }

    @RequestMapping(value = "/emp", method = RequestMethod.PUT)
    public String update(Employee employee) {
        employeeDao.save(employee);
        return "redirect:/emps";
    }

    @RequestMapping(value = "/emp/{id}", method = RequestMethod.GET)
    public String get(@PathVariable(value = "id", required = false) Integer id, ModelMap map) {
        map.put("departments", departmentDao.getDepartments());
        map.put("employee", employeeDao.getEmployeeById(id));
        return "input";
    }

    @RequestMapping(value = "/emp/{id}", method = RequestMethod.DELETE)
    public String delete(@PathVariable(value = "id", required = false) Integer id) {
        employeeDao.delete(id);
        return "redirect:/emps";
    }

    @RequestMapping(value = "/emp", method = RequestMethod.POST)
    public String save(Employee employee) {
        System.out.println("going to save employee: " + employee);
        Department department = departmentDao.getDepartmentById(employee.getDepartment().getId());
        employee.setDepartment(department);
        employeeDao.save(employee);
        return "redirect:/emps";//重定向到list页面s
    }

    /**
     * 自定义类型转换器的测试
     */
    @RequestMapping(value = "/convert", method = RequestMethod.POST)
    public String convert(@Valid @RequestParam("employee") Employee employee, BindingResult bindingResult) {
        System.out.println("employee from str " + employee);
        System.out.println(bindingResult.getAllErrors().toString());
//        可以通过bindingResult 对象获取数据绑定的结果, 可知道 数据转换,验证是否有错,以此告知前台
        //或者转向定制的错误页面
        employeeDao.save(employee);
        return "redirect:/emps";
    }
}
